package com.hippo.core.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;

/**
 * @ClassName ScheduleConfig
 * @Description TODO spring定时任务配置类
 * @Author tangxl
 * @create 2023-04-28 11:32
 **/
@Configuration
public class ScheduleConfig {

    // 用来存入线程执行情况, 方便于停止定时任务时使用
    public static ConcurrentHashMap<String, ScheduledFuture> cache= new ConcurrentHashMap<String, ScheduledFuture>();

    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler(){
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        // 设置线程池容量-线程池大小最好是2的指数倍
        threadPoolTaskScheduler.setPoolSize(10);
        // 线程名前缀-方便我们定位处理任务所在的线程池
        threadPoolTaskScheduler.setThreadNamePrefix("task-thread-pool-");
        // 等待时常-等待时常过后强制关闭
        threadPoolTaskScheduler.setAwaitTerminationSeconds(60);
        // 当线程池关闭时等待所有任务完成-是否等待所有任务完成后再继续销毁其他的Bean
        threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
        return threadPoolTaskScheduler;
    }
}
